<?php
function getNumber($string){
    $number  =   preg_replace('/[^0-9]/','',$string);
    if($number == ''){
        $number = 0;
    }
    return $number  =   intval($number);
}

function getProductShopProperty($id) {
    $rs = array();
    $db = db_query("SELECT * FROM {raovat_property} WHERE rid = %d",$id);
    if ($db) {
        while ($row = db_fetch_array($db)) {
            $rs[] = $row;
            $rs["val_" . $row["filter_id"]] = $row["property_name"];
        }
        return $rs;
    } else {
        echo "Câu lệnh sql chưa đúng";
    }
}

function insertDrupal($table, $arrayType, $arrayValue) {
    $sql = 'INSERT INTO ' . $table . ' (' . implode(', ', array_keys($arrayType)) . ') VALUES (' . implode(', ', $arrayType) . ')';
    return db_query($sql, $arrayValue);
}

function getAttributes($cate_id){
    $sql = "SELECT * FROM {category_filter_name} LEFT JOIN {category_filter_group} ON filter_group = group_id WHERE {category_filter_group}.cate_id = %d";
    $db = db_query($sql,$cate_id);
    while($row = db_fetch_array($db)){
        $rs[$row['group_id']]["key"][$row['filter_id']] = $row; 
        $rs[$row['group_id']]["value"] = $row['group_name'];
    }
    return $rs;
}

function getAttributesValue($id){
    $sql = "SELECT * FROM {category_filter_property} WHERE filter_id = %d";
    $db_ex = db_query($sql,$id);
    while($row = db_fetch_array($db_ex)){
        $rs[] = $row;
    }
    return $rs;
}

function deleteRow($table,$field_id,$field_value){
    $sql = "DELETE FROM {" . $table . "} WHERE " . $field_id . " = %d";
    return db_query($sql,$field_value);
}

/**
 * Number of days in a month
 *
 * Takes a month/year as input and returns the number of days
 * for the given month/year. Takes leap years into consideration.
 * @param    integer a numeric month  - $month
 * @param    integer    a numeric year - $year
 * @return    integer
 */
if ( ! function_exists('days_in_month'))
{
    function days_in_month($month = 0, $year = '')
    {
        if ($month < 1 OR $month > 12)
        {
            return 0;
        }
        
        if ( ! is_numeric($year) OR strlen($year) != 4)
        {
            $year = date('Y');
        }
        
        if ($month == 2)
        {
            if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
            {
                return 29;
            }
        }
        
        $days_in_month    = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
        return $days_in_month[$month - 1];
    }
}

/** Ham lay ra thong tin cate theo ID
 *  $cate_id = string(s) or int(1) or null(all)
 *  $cate_id = string must be space by ','
 */
function getRecordById($tbl_name, $field_id, $str_id) {
    $sql        = "SELECT * FROM {". $tbl_name ."} WHERE ". $field_id ." IN(0,'%s')";
    $result     = db_query($sql, strval($str_id));
    while($row  = db_fetch_array($result)){            
        $arrayReturn[$row[$field_id]] = $row;
    }
    return $arrayReturn;
}

/** Function get value from form */
function getValue($value_name, $data_type = "int", $method = "GET", $default_value = 0, $advance = 0){
	$value = $default_value;
	switch($method){
		case "GET": if(isset($_GET[$value_name])) $value = $_GET[$value_name]; break;
		case "POST": if(isset($_POST[$value_name])) $value = $_POST[$value_name]; break;
		case "COOKIE": if(isset($_COOKIE[$value_name])) $value = $_COOKIE[$value_name]; break;
		case "SESSION": if(isset($_SESSION[$value_name])) $value = $_SESSION[$value_name]; break;
		default: if(isset($_GET[$value_name])) $value = $_GET[$value_name]; break;
	}
	$valueArray	= array("int" => intval($value), "str" => trim(strval($value)), "flo" => floatval($value), "dbl" => doubleval($value), "arr" => $value);
	foreach($valueArray as $key => $returnValue){
		if($data_type == $key){
			if($advance != 0){
				switch($advance){
					case 1:
						$returnValue = replaceMQ($returnValue);
						break;
					case 2:
						$returnValue = htmlspecialbo($returnValue);
						break;
				}
			}
			//Do số quá lớn nên phải kiểm tra trước khi trả về giá trị
			if((strval($returnValue) == "INF") && ($data_type != "str")) return 0;
			return $returnValue;
			break;
		}
	}
	return (intval($value));
}

/** Function get all of record in table */
function getAllRecord($tbl_name,$id_field) {
    $sql = "SELECT * FROM {" . $tbl_name . "}";
    
    $result = db_query($sql);
    while($row = db_fetch_array($result)) {
        $arrayReturn[$row[$id_field]] = $row;
    }
    return $arrayReturn;
}

/** Function get all of record in location table */
function getAllLocationInfo() {
    
    $arrayReturn = array();
    $sql    = "SELECT * FROM {location}";
    
    $result = db_query($sql);
    while($row = db_fetch_array($result)){
        $arrayReturn[] = $row;
    }
    
    return $arrayReturn;
}

/** Function cut a string */
function cut_string($str, $length, $char=" ..."){
    
	//Nếu chuỗi cần cắt nhỏ hơn $length thì return luôn
	$strlen	= mb_strlen($str, "UTF-8");
	if($strlen <= $length) return $str;
	
	//Cắt chiều dài chuỗi $str tới đoạn cần lấy
	$substr	= mb_substr($str, 0, $length, "UTF-8");
	if(mb_substr($str, $length, 1, "UTF-8") == " ") return $substr . $char;
	
	//Xác định dấu " " cuối cùng trong chuỗi $substr vừa cắt
	$strPoint= mb_strrpos($substr, " ", "UTF-8");
	
	//Return string
	if($strPoint < $length - 20) return $substr . $char;
	else return mb_substr($substr, 0, $strPoint, "UTF-8") . $char;
}

/** Function insert a record into raovat table */
function addRaovat($type=array(), $value=array()) {
    $sql    = 'INSERT INTO {raovat} ('. implode(', ', array_keys($type)) .') VALUES ('. implode(', ', $type) .')';
    $db     = db_query($sql, $value);
    return db_last_insert_id('cm_raovat', 'rid');
}

/** Function get subcategory */
function getSubcate($parent_id=0) {
    $arrayReturn = array();
    $sql    = 'SELECT * FROM {category} WHERE cate_parent = %d';
    $result = db_query($sql, $parent_id);
    while($row  = db_fetch_array($result)) {
        $arrayReturn[] = $row;
    }
    return $arrayReturn;
}

/** Function edit a record into raovat table */
function updateRaovat($type=array(), $value=array(), $rid) {
    $sql = "UPDATE {raovat} SET ";
    foreach($type as $index=>$val) {
        $sql .= $index . "=" . $val . ",";
    }
    $sql = substr($sql, 0, strlen($sql)-1);
    $sql .= " WHERE rid = " . $rid;
    return db_query($sql, $value);
}

/** Function block a record into raovat table */
function blockRaovat($rid) {
    $sql = 'UPDATE {raovat} SET status = %d WHERE rid = %d';
    return db_query($sql, 0, $rid);
}

/** Function unblock a record into raovat table */
function unblockRaovat($rid) {
    $sql = 'UPDATE {raovat} SET status = %d WHERE rid = %d';
    return db_query($sql, 1, $rid);
}

/** Function check a record has been blocked or not */
function checkRaovat($rid) {
    $sql    = 'SELECT status FROM {raovat} WHERE rid = %d';
    $result = db_query($sql, $rid);
    $row    = db_fetch_array($result);
    return $row["status"];
}

/** Function convert date(str) to time(int) */
function convertDateTime($strDate = "", $strTime = ""){
	//Break string and create array date time
	$array_replace	= array("/", ":");
	$strDate		= str_replace($array_replace, "-", $strDate);
	$strTime		= str_replace($array_replace, "-", $strTime);
	$strDateArray	= explode("-", $strDate);
	$strTimeArray	= explode("-", $strTime);
	$countDateArr	= count($strDateArray);
	$countTimeArr	= count($strTimeArray);
	
	//Get Current date time
	$today			= getdate();
	$day				= $today["mday"];
	$mon				= $today["mon"];
	$year				= $today["year"];
	$hour				= $today["hours"];
	$min				= $today["minutes"];
	$sec				= $today["seconds"];
	//Get date array
	switch($countDateArr){
		case 2:
			$day		= intval($strDateArray[0]);
			$mon		= intval($strDateArray[1]);
			break;
		case $countDateArr >= 3:
			$day		= intval($strDateArray[0]);
			$mon		= intval($strDateArray[1]);
			$year 	= intval($strDateArray[2]);
			break;
	}
	//Get time array
	switch($countTimeArr){
		case 2:
			$hour		= intval($strTimeArray[0]);
			$min		= intval($strTimeArray[1]);
			break;
		case $countTimeArr >= 3:
			$hour		= intval($strTimeArray[0]);
			$min		= intval($strTimeArray[1]);
			$sec		= intval($strTimeArray[2]);
			break;
	}
	//Return date time integer
	if(@mktime($hour, $min, $sec, $mon, $day, $year) == -1) return $today[0];
	else return mktime($hour, $min, $sec, $mon, $day, $year);
}
?>